library(tidyverse)
library(haven)
library(ggridges)
library(ggpubr)
library (readstata13)
library (ggpattern)

#Set working directory
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

#Upload MergedCleanedDataset which is the merged file of 2016 and 2020 ANES and has standardized versions of all variables
ALL_MERGED_ANES <- read_stata ("MergedandCleanedDataset.dta")
#View(ALL_MERGED_ANES)

#2016 and 2020 subsets: 

ALL_MERGED_ANES_2016 <-subset(ALL_MERGED_ANES, year ==2016)
ALL_MERGED_ANES_2020 <-subset(ALL_MERGED_ANES, year ==2020)

################################################################################
#Figure 1: Bar Graph of White Identity and Party (all years)
################################################################################

#Create a data subset that omits missing data
fig1_sub <- filter(ALL_MERGED_ANES, !is.na(whiteindex), !is.na(republican)) %>%
  mutate(whiteindexfactor = factor(round(whiteindex, 2)), 
         pid = case_when(republican == 1 ~ "Republican",
                         republican == 0 ~ "Democrat")) 
  

fig1 <- ggplot(data = fig1_sub, aes(whiteindexfactor, fill = pid, colour = pid)) +
  geom_bar_pattern(aes(whiteindexfactor, pattern = pid),
                   position = "dodge",
                   pattern_fill = "white",
                   pattern_color = "white", 
                   pattern_spacing = .02,
                   pattern_density = .1) +
  scale_fill_manual(values = c("blue", "red")) + 
  scale_y_continuous(limits = c(0, 525), breaks = c(0, 100, 200, 300, 400, 500, 600)) +
  scale_colour_manual(values = c("blue", "red")) +
  labs(x = "White Identity Index", y = "Frequency") +
  theme_bw() + 
  theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), 
        legend.position = c(.85, .8), text = element_text(family = "serif"),
        legend.title = element_blank())
fig1

ggsave(fig1, filename = "Figures/Figure1.tiff", height = 2.5, width = 6)



################################################################################
#Figure 2: Ideology by White Identity and Party (all years)
################################################################################

ALL_MERGED_ANES_dem <- filter(ALL_MERGED_ANES, republican == 0)
ALL_MERGED_ANES_dem <- filter(ALL_MERGED_ANES_dem, !is.na(ideo7), !is.na(highwhite))
ALL_MERGED_ANES_dem$highwhitebinary <- factor(ALL_MERGED_ANES_dem$highwhite)
ALL_MERGED_ANES_dem <- mutate(ALL_MERGED_ANES_dem, highwhitefourth = if_else(whiteindex <=.25, "Low White Identity", if_else(whiteindex >=.75, "High White Identity", NA_character_)))
ALL_MERGED_ANES_dem <- filter(ALL_MERGED_ANES_dem, !is.na(highwhitefourth))

dem_density <- ggplot(data = ALL_MERGED_ANES_dem, aes(x = ideo7, fill = highwhitefourth)) +
  geom_density(alpha = .5, adjust = 2.5) + 
  theme_bw() + 
  labs(y = "", x = "Self-Reported Ideology", title = "Democrats") +
  scale_fill_grey(start = 0.1, end = 0.9) +
  scale_x_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7), labels = c("1", "2", "3", "4", "5", "6", "7")) +
  scale_y_continuous(limits = c(0, 0.5), breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5)) +
  theme(legend.title = element_blank(), legend.position = "none", text = element_text(family = "serif"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.text.x = element_text(angle = 0, hjust = 1))
dem_density

ALL_MERGED_ANES_rep <- filter(ALL_MERGED_ANES, republican == 1)
ALL_MERGED_ANES_rep <- filter(ALL_MERGED_ANES_rep, !is.na(ideo7), !is.na(highwhite))
ALL_MERGED_ANES_rep$highwhitebinary <- factor(ALL_MERGED_ANES_rep$highwhite)
ALL_MERGED_ANES_rep <- mutate(ALL_MERGED_ANES_rep, highwhitefourth = if_else(whiteindex <=.25, "Low White Identity", if_else(whiteindex >=.75, "High White Identity", NA_character_)))
ALL_MERGED_ANES_rep <- filter(ALL_MERGED_ANES_rep, !is.na(highwhitefourth))

rep_density <- ggplot(data = ALL_MERGED_ANES_rep, aes(x = ideo7, fill = highwhitefourth)) +
  geom_density(alpha = .5, adjust = 2.5) +
  theme_bw() +
  labs(y = "", x = "Self-Reported Ideology", title = "Republicans") +
  scale_fill_grey(start = 0.1, end = 0.9) +
  scale_x_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7), labels = c("1", "2", "3", "4", "5", "6", "7")) +
  scale_y_continuous(limits = c(0, 0.5), breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5)) +
  theme(legend.title = element_blank(), legend.position = "none", text = element_text(family = "serif"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.text.x = element_text(angle = 0, hjust = 1))
rep_density

ALL_MERGED_ANES_dem_op <- filter(ALL_MERGED_ANES, republican == 0)
ALL_MERGED_ANES_dem_op <- filter(ALL_MERGED_ANES_dem_op, !is.na(operational_ideology), !is.na(highwhite))
ALL_MERGED_ANES_dem_op$highwhitebinary <- factor(ALL_MERGED_ANES_dem_op$highwhite)
ALL_MERGED_ANES_dem_op <- mutate(ALL_MERGED_ANES_dem_op, highwhitefourth = if_else(whiteindex <=.25, "Low White Identity", if_else(whiteindex >=.75, "High White Identity", NA_character_)))
ALL_MERGED_ANES_dem_op <- filter(ALL_MERGED_ANES_dem_op, !is.na(highwhitefourth))

dem_density_op <- ggplot(data = ALL_MERGED_ANES_dem_op, aes(x = operational_ideology, fill = highwhitefourth)) +
  geom_density(alpha = .5, adjust = 2.5) + 
  theme_bw() + 
  labs(y = "", x = "Operational Ideology", title = "Democrats") +
  scale_fill_grey(start = 0.1, end = 0.9) +
  scale_x_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7), labels = c("1", "2", "3", "4", "5", "6", "7")) +
  scale_y_continuous(limits = c(0, 0.5), breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5)) +
  theme(legend.title = element_blank(), text = element_text(family = "serif"), 
        legend.position = c(.5, .8), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), 
        axis.text.x = element_text(angle = 0, hjust = 1))
dem_density_op

ALL_MERGED_ANES_rep_op <- filter(ALL_MERGED_ANES, republican == 1)
ALL_MERGED_ANES_rep_op <- filter(ALL_MERGED_ANES_rep_op, !is.na(operational_ideology), !is.na(highwhite))
ALL_MERGED_ANES_rep_op$highwhitebinary <- factor(ALL_MERGED_ANES_rep_op$highwhite)
ALL_MERGED_ANES_rep_op <- mutate(ALL_MERGED_ANES_rep_op, highwhitefourth = if_else(whiteindex <=.25, "Low White Identity", if_else(whiteindex >=.75, "High White Identity", NA_character_)))
ALL_MERGED_ANES_rep_op <- filter(ALL_MERGED_ANES_rep_op, !is.na(highwhitefourth))

rep_density_op <- ggplot(data = ALL_MERGED_ANES_rep_op, aes(x = operational_ideology, fill = highwhitefourth)) +
  geom_density(alpha = .5, adjust = 2.5) +
  theme_bw() +
  labs(y = "", x = "Operational Ideology", title = "Republicans") +
  scale_fill_grey(start = 0.1, end = 0.9) +
  scale_x_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7), labels =c("1", "2", "3", "4", "5", "6", "7")) +
  scale_y_continuous(limits = c(0, 0.5), breaks = c(0, 0.1, 0.2, 0.3, 0.4, 0.5)) +
  theme(legend.title = element_blank(), legend.position = "none",  text = element_text(family = "serif"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.text.x = element_text(angle = 0, hjust = 1))
rep_density_op



#Put the two into one graph
fig2 <- ggarrange(dem_density_op, rep_density_op, dem_density, rep_density,
                  labels = c("A", "B", "C", "D"),
          ncol = 4, nrow = 1)
fig2

ggsave(fig2, filename = "Figures/Figure2.tiff", height = 3, width = 9)

################################################################################
#Figure 3: Relationship Between White Identity and Various Attitudes, by Party (all years)
################################################################################

#NATIONALISM
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_nationalism <- select(ALL_MERGED_ANES, republican, whiteindex, nationindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), nationindex_mean = mean(nationindex), nationindex_sd = sd(nationindex)) %>%
  mutate(nationindex_se = nationindex_sd/sqrt(n)) %>%
  mutate(nationindex_confhi = nationindex_mean + (nationindex_se*1.96), 
         nationindex_conflow = nationindex_mean - (nationindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_nationalism$republican <- factor(group_party_binary_nationalism$republican)

#Create a smoothed loess line with shaded error region 
nationalism_loess <- ggplot(data = group_party_binary_nationalism, aes(y = nationindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
 # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  #guides(color = FALSE, linetype = FALSE) +
  labs(title = "National Identity", x = element_blank(), y = "Mean Issue Position") + 
  scale_colour_manual(values = c("blue", "red"), labels = c("Democrat", "Republican"), guide = guide_legend(reverse = TRUE)) +
  scale_linetype_manual(values = c(1, 2), labels = c("Democrat", "Republican"), guide = guide_legend(reverse = TRUE)) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = c(.65, .2), legend.title = element_blank())

nationalism_loess

#RACIAL RESENTMENT
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_racial <- select(ALL_MERGED_ANES, republican, whiteindex, resentindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), resentindex_mean = mean(resentindex), resentindex_sd = sd(resentindex)) %>%
  mutate(resentindex_se = resentindex_sd/sqrt(n)) %>%
  mutate(resentindex_confhi = resentindex_mean + (resentindex_se*1.96), 
         resentindex_conflow = resentindex_mean - (resentindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_racial$republican <- factor(group_party_binary_racial$republican)

#Create a smoothed loess line with shaded error region 
racial_loess <- ggplot(data = group_party_binary_racial, aes(y = resentindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Racial Resentment", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red"))+
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")
racial_loess

#IMMIGRATION
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_immigration <- select(ALL_MERGED_ANES, republican, whiteindex, immigrationindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), immigrationindex_mean = mean(immigrationindex), immigrationindex_sd = sd(immigrationindex)) %>%
  mutate(immigrationindex_se = immigrationindex_sd/sqrt(n)) %>%
  mutate(immigrationindex_confhi = immigrationindex_mean + (immigrationindex_se*1.96), 
         immigrationindex_conflow = immigrationindex_mean - (immigrationindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_immigration$republican <- factor(group_party_binary_immigration$republican)

#Create a smoothed loess line with shaded error region 
immigration_loess <- ggplot(data = group_party_binary_immigration, aes(y = immigrationindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Immigration Policy", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#WOMEN
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_women <- select(ALL_MERGED_ANES, republican, whiteindex, womenindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), womenindex_mean = mean(womenindex), womenindex_sd = sd(womenindex)) %>%
  mutate(womenindex_se = womenindex_sd/sqrt(n)) %>%
  mutate(womenindex_confhi = womenindex_mean + (womenindex_se*1.96), 
         womenindex_conflow = womenindex_mean - (womenindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_women$republican <- factor(group_party_binary_women$republican)


#Create a smoothed loess line with shaded error region 
women_loess <- ggplot(data = group_party_binary_women, aes(y = womenindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Women's Equality", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#CLIMATE
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_climate <- select(ALL_MERGED_ANES, republican, whiteindex, climateindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), climateindex_mean = mean(climateindex), climateindex_sd = sd(climateindex)) %>%
  mutate(climateindex_se = climateindex_sd/sqrt(n)) %>%
  mutate(climateindex_confhi = climateindex_mean + (climateindex_se*1.96), 
         climateindex_conflow = climateindex_mean - (climateindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_climate$republican <- factor(group_party_binary_climate$republican)

group_party_binary_climate <- mutate(group_party_binary_climate, partyname = if_else(republican == 0, "Democrat", "Republican"))

#Create a smoothed loess line with shaded error region 
climate_loess <- ggplot(data = group_party_binary_climate, aes(y = climateindex_mean, x = whiteindex, color = partyname, linetype = partyname)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  #guides(color = FALSE) +
  scale_colour_manual(values = c("blue", "red")) +
  scale_linetype_manual(values = c("solid", "dashed")) +
  labs(title = "Climate Change", x = "White Identity Index", y = "Mean Issue Position", colour = element_blank(), linetype = element_blank()) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#LGBTQ+ 
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_lgbtq <- select(ALL_MERGED_ANES, republican, whiteindex, lgbtqindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), lgbtqindex_mean = mean(lgbtqindex), lgbtqindex_sd = sd(lgbtqindex)) %>%
  mutate(lgbtqindex_se = lgbtqindex_sd/sqrt(n)) %>%
  mutate(lgbtqindex_confhi = lgbtqindex_mean + (lgbtqindex_se*1.96), 
         lgbtqindex_conflow = lgbtqindex_mean - (lgbtqindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_lgbtq$republican <- factor(group_party_binary_lgbtq$republican)

#Create a smoothed loess line with shaded error region 
lgbtq_loess <- ggplot(data = group_party_binary_lgbtq, aes(y = lgbtqindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "LGBTQ+", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#MEDICAL INSURANCE
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_medinsur <- select(ALL_MERGED_ANES, republican, whiteindex, medinsurindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), medinsurindex_mean = mean(medinsurindex), medinsurindex_sd = sd(medinsurindex)) %>%
  mutate(medinsurindex_se = medinsurindex_sd/sqrt(n)) %>%
  mutate(medinsurindex_confhi = medinsurindex_mean + (medinsurindex_se*1.96), 
         medinsurindex_conflow = medinsurindex_mean - (medinsurindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_medinsur$republican <- factor(group_party_binary_medinsur$republican)

#Create a smoothed loess line with shaded error region
medinsur_loess <- ggplot(data = group_party_binary_medinsur, aes(y = medinsurindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "Medical Insurance", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#ABORTION
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_binary_abortion <- select(ALL_MERGED_ANES, republican, whiteindex, abortionindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), abortionindex_mean = mean(abortionindex), abortionindex_sd = sd(abortionindex)) %>%
  mutate(abortionindex_se = abortionindex_sd/sqrt(n)) %>%
  mutate(abortionindex_confhi = abortionindex_mean + (abortionindex_se*1.96), 
         abortionindex_conflow = abortionindex_mean - (abortionindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_binary_abortion$republican <- factor(group_party_binary_abortion$republican)

#Create a smoothed loess line with shaded error region 
abortion_loess <- ggplot(data = group_party_binary_abortion, aes(y = abortionindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth() +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "Abortion", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#Put the many loess lines into one graph
fig3 <- ggarrange(nationalism_loess, racial_loess, immigration_loess, women_loess, climate_loess, lgbtq_loess, medinsur_loess, abortion_loess,
          labels = c("A", "B", "C", "D", "E", "F", "G", "H"),
          ncol = 4, nrow = 2)
fig3

ggsave(fig3, filename = "Figures/Figure3.tiff", height = 5, width = 9)

################################################################################
#Figure B1: Ideological Distribution by White Identity Level – Full Sample
################################################################################
table(ALL_MERGED_ANES$whiteindex)

whiteindexhist <- ggplot(ALL_MERGED_ANES, aes(x = whiteindex)) +
  geom_bar(width = .06, fill = "grey70", color = "black") +
  labs(x = "White Identity Index", y = "Frequency", title = NULL) +
  theme_bw() +
  theme(text = element_text(family = "serif"), panel.grid = element_blank()) +
  ylim (0, 1500) +
  xlim (0, 1) +
  scale_x_continuous(breaks = c(0, 0.08, 0.17, 0.25, 0.33, 0.42, 0.50, 0.58, 0.66, 0.75, 0.83, 0.92, 1)) +
  scale_y_continuous(breaks = c(0,300, 600, 900, 1200))

################################################################################
#Figure B2: Ideological Distribution by White Identity Level – 2016 Data
################################################################################
                     
#Create a data subset that omits missing data
fig1_sub_2016 <- filter(ALL_MERGED_ANES_2016, !is.na(whiteindex), !is.na(republican)) %>%
  mutate(whiteindexfactor = factor(round(whiteindex, 2)), 
         pid = case_when(republican == 1 ~ "Republican",
                         republican == 0 ~ "Democrat")) 


figb2 <- ggplot(data = fig1_sub_2016, aes(whiteindexfactor, fill = pid, colour = pid)) +
  geom_bar_pattern(aes(whiteindexfactor, pattern = pid),
                   position = "dodge",
                   pattern_fill = "white",
                   pattern_color = "white", 
                   pattern_spacing = .02,
                   pattern_density = .1) +
  scale_fill_manual(values = c("blue", "red")) + 
  scale_y_continuous(limits = c(0, 400), breaks = c(0, 100, 200, 300, 400, 500, 600)) +
  scale_colour_manual(values = c("blue", "red")) +
  labs(x = "White Identity Index", y = "Frequency") +
  theme_bw() + 
  theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), 
        legend.position = c(.85, .8), text = element_text(family = "serif"),
        legend.title = element_blank())
figb2

ggsave(figb2, filename = "Figures/FigureB2.tiff", height = 2.5, width = 6)


################################################################################
#Figure B3: Ideological Distribution by White Identity Level – 2016 Data
################################################################################

#Create a data subset that omits missing data
fig1_sub_2020 <- filter(ALL_MERGED_ANES_2020, !is.na(whiteindex), !is.na(republican)) %>%
  mutate(whiteindexfactor = factor(round(whiteindex, 2)), 
         pid = case_when(republican == 1 ~ "Republican",
                         republican == 0 ~ "Democrat")) 


figb3 <- ggplot(data = fig1_sub_2020, aes(whiteindexfactor, fill = pid, colour = pid)) +
  geom_bar_pattern(aes(whiteindexfactor, pattern = pid),
                   position = "dodge",
                   pattern_fill = "white",
                   pattern_color = "white", 
                   pattern_spacing = .02,
                   pattern_density = .1) +
  scale_fill_manual(values = c("blue", "red")) + 
  scale_y_continuous(limits = c(0, 400), breaks = c(0, 100, 200, 300, 400, 500, 600)) +
  scale_colour_manual(values = c("blue", "red")) +
  labs(x = "White Identity Index", y = "Frequency") +
  theme_bw() + 
  theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank(), 
        legend.position = c(.85, .8), text = element_text(family = "serif"),
        legend.title = element_blank())
figb3

ggsave(figb3, filename = "Figures/FigureB3.tiff", height = 2.5, width = 6)

################################################################################
#Figure C1: Ideological Distribution by White Identity Level – Full Sample
################################################################################
ideo_dist_data <-ALL_MERGED_ANES %>%
  filter(!is.na(highwhite)) %>%
  filter(!is.na(ideo7))

summary_data_c1 <- ideo_dist_data %>%
  group_by(highwhite, ideo7) %>%
  summarise(Frequency = n()) %>%
  mutate(Percentage = Frequency / sum(Frequency))

fig_c1 <- ggplot(summary_data_c1, aes(x = factor(ideo7), y=Percentage, fill = factor(highwhite))) +
  geom_bar(stat="identity", position = position_dodge(width = .6), width = 0.6) +
  labs(x = NULL, y = "Percent", title = NULL) +
  theme_bw() +
  theme(text = element_text(family = "serif"), panel.grid = element_blank(),
        legend.position = "bottom") +
  scale_fill_manual(values= c("1"="grey40","0"="grey80"),
                    labels= c("0"="Low White Identity","1"="High White Identity"),
                    name= NULL)+
  coord_cartesian(ylim = c(0,.51))+
  scale_x_discrete(labels=c("1"="Extremely\nLiberal", "2"="Liberal", "3"="Slightly\nLiberal",
                            "4"="Moderate;\nmiddle of the\nroad", "5"="Slightly\nConservative",
                            "6"="Conservative", "7"="Extremely\nConservative"))+
  scale_y_continuous( labels =scales::percent,
                      expand= c(0,0))

ggsave(fig_c1, filename = "Figures/FigureC1.tiff", height = 4, width = 7)

################################################################################
#Figure C2: Republican Ideological Distribution by White Identity Level
################################################################################
ideo_dist_data_rep <-subset(ALL_MERGED_ANES, republican==1) %>%
  filter(!is.na(highwhite)) %>%
  filter(!is.na(ideo7))

summary_data_c2 <- ideo_dist_data_rep %>%
  group_by(highwhite, ideo7) %>%
  summarise(Frequency = n()) %>%
  mutate(Percentage = Frequency / sum(Frequency))

fig_c2 <- ggplot(summary_data_c2, aes(x = factor(ideo7), y=Percentage, fill = factor(highwhite))) +
  geom_bar(stat="identity", position = position_dodge(width = .6), width = 0.6) +
  labs(x = NULL, y = "Percent", title = NULL) +
  theme_bw() +
  theme(text = element_text(family = "serif"), panel.grid = element_blank(),
        legend.position = "bottom") +
  scale_fill_manual(values= c("1"="grey40","0"="grey80"),
                    labels= c("0"="Low White Identity","1"="High White Identity"),
                    name= NULL)+
  coord_cartesian(ylim = c(0,.51))+
  scale_x_discrete(labels=c("1"="Extremely\nLiberal", "2"="Liberal", "3"="Slightly\nLiberal",
                            "4"="Moderate;\nmiddle of the\nroad", "5"="Slightly\nConservative",
                            "6"="Conservative", "7"="Extremely\nConservative"))+
  scale_y_continuous( labels =scales::percent,
                      expand= c(0,0))

ggsave(fig_c2, filename = "Figures/FigureC2.tiff", height = 4, width = 7)

################################################################################
#Figure C3: Democrat Ideological Distribution by White Identity Level
################################################################################
ideo_dist_data_dem <-subset(ALL_MERGED_ANES, republican==0) %>%
  filter(!is.na(highwhite)) %>%
  filter(!is.na(ideo7))

summary_data_c3 <- ideo_dist_data_dem %>%
  group_by(highwhite, ideo7) %>%
  summarise(Frequency = n()) %>%
  mutate(Percentage = Frequency / sum(Frequency))

fig_c3 <- ggplot(summary_data_c3, aes(x = factor(ideo7), y=Percentage, fill = factor(highwhite))) +
  geom_bar(stat="identity", position = position_dodge(width = .6), width = 0.6) +
  labs(x = NULL, y = "Percent", title = NULL) +
  theme_bw() +
  theme(text = element_text(family = "serif"), panel.grid = element_blank(),
        legend.position = "bottom") +
  scale_fill_manual(values= c("1"="grey40","0"="grey80"),
                    labels= c("0"="Low White Identity","1"="High White Identity"),
                    name= NULL)+
  coord_cartesian(ylim = c(0,.51))+
  scale_x_discrete(labels=c("1"="Extremely\nLiberal", "2"="Liberal", "3"="Slightly\nLiberal",
                            "4"="Moderate;\nmiddle of the\nroad", "5"="Slightly\nConservative",
                            "6"="Conservative", "7"="Extremely\nConservative"))+
  scale_y_continuous( labels =scales::percent,
                      expand= c(0,0))

ggsave(fig_c3, filename = "Figures/FigureC3.tiff", height = 4, width = 7)

####################################################################
#Figure D1: Issue Attitudes by White Identity, 2016 Data
################################################################################

#NATIONALISM
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_nationalism <- select(ALL_MERGED_ANES_2016, republican, whiteindex, nationindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), nationindex_mean = mean(nationindex), nationindex_sd = sd(nationindex)) %>%
  mutate(nationindex_se = nationindex_sd/sqrt(n)) %>%
  mutate(nationindex_confhi = nationindex_mean + (nationindex_se*1.96), 
         nationindex_conflow = nationindex_mean - (nationindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_nationalism$republican <- factor(group_party_2016_binary_nationalism$republican)

#Create a smoothed loess line with shaded error region 
nationalism_loess_2016 <- ggplot(data = group_party_2016_binary_nationalism, aes(y = nationindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  #guides(color = FALSE, linetype = FALSE) +
  labs(title = "National Identity", x = element_blank(), y = "Mean Issue Position") + 
  scale_colour_manual(values = c("blue", "red"), labels = c("Democrat", "Republican"), guide = guide_legend(reverse = TRUE)) +
  scale_linetype_manual(values = c(1, 2), labels = c("Democrat", "Republican"), guide = guide_legend(reverse = TRUE)) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = c(.65, .2), legend.title = element_blank())

nationalism_loess_2016

#RACIAL RESENTMENT
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_racial <- select(ALL_MERGED_ANES_2016, republican, whiteindex, resentindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), resentindex_mean = mean(resentindex), resentindex_sd = sd(resentindex)) %>%
  mutate(resentindex_se = resentindex_sd/sqrt(n)) %>%
  mutate(resentindex_confhi = resentindex_mean + (resentindex_se*1.96), 
         resentindex_conflow = resentindex_mean - (resentindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_racial$republican <- factor(group_party_2016_binary_racial$republican)

#Create a smoothed loess line with shaded error region 
racial_loess_2016 <- ggplot(data = group_party_2016_binary_racial, aes(y = resentindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Racial Resentment", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red"))+
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")
racial_loess_2016

#IMMIGRATION
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_immigration <- select(ALL_MERGED_ANES_2016, republican, whiteindex, immigrationindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), immigrationindex_mean = mean(immigrationindex), immigrationindex_sd = sd(immigrationindex)) %>%
  mutate(immigrationindex_se = immigrationindex_sd/sqrt(n)) %>%
  mutate(immigrationindex_confhi = immigrationindex_mean + (immigrationindex_se*1.96), 
         immigrationindex_conflow = immigrationindex_mean - (immigrationindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_immigration$republican <- factor(group_party_2016_binary_immigration$republican)

#Create a smoothed loess line with shaded error region 
immigration_loess_2016 <- ggplot(data = group_party_2016_binary_immigration, aes(y = immigrationindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Immigration Policy", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#WOMEN
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_women <- select(ALL_MERGED_ANES_2016, republican, whiteindex, womenindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), womenindex_mean = mean(womenindex), womenindex_sd = sd(womenindex)) %>%
  mutate(womenindex_se = womenindex_sd/sqrt(n)) %>%
  mutate(womenindex_confhi = womenindex_mean + (womenindex_se*1.96), 
         womenindex_conflow = womenindex_mean - (womenindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_women$republican <- factor(group_party_2016_binary_women$republican)


#Create a smoothed loess line with shaded error region 
women_loess_2016 <- ggplot(data = group_party_2016_binary_women, aes(y = womenindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Women's Equality", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#CLIMATE
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_climate <- select(ALL_MERGED_ANES_2016, republican, whiteindex, climateindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), climateindex_mean = mean(climateindex), climateindex_sd = sd(climateindex)) %>%
  mutate(climateindex_se = climateindex_sd/sqrt(n)) %>%
  mutate(climateindex_confhi = climateindex_mean + (climateindex_se*1.96), 
         climateindex_conflow = climateindex_mean - (climateindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_climate$republican <- factor(group_party_2016_binary_climate$republican)

group_party_2016_binary_climate <- mutate(group_party_2016_binary_climate, partyname = if_else(republican == 0, "Democrat", "Republican"))

#Create a smoothed loess line with shaded error region 
climate_loess_2016 <- ggplot(data = group_party_2016_binary_climate, aes(y = climateindex_mean, x = whiteindex, color = partyname, linetype = partyname)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  #guides(color = FALSE) +
  scale_colour_manual(values = c("blue", "red")) +
  scale_linetype_manual(values = c("solid", "dashed")) +
  labs(title = "Climate Change", x = "White Identity Index", y = "Mean Issue Position", colour = element_blank(), linetype = element_blank()) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#LGBTQ+ 
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_lgbtq <- select(ALL_MERGED_ANES_2016, republican, whiteindex, lgbtqindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), lgbtqindex_mean = mean(lgbtqindex), lgbtqindex_sd = sd(lgbtqindex)) %>%
  mutate(lgbtqindex_se = lgbtqindex_sd/sqrt(n)) %>%
  mutate(lgbtqindex_confhi = lgbtqindex_mean + (lgbtqindex_se*1.96), 
         lgbtqindex_conflow = lgbtqindex_mean - (lgbtqindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_lgbtq$republican <- factor(group_party_2016_binary_lgbtq$republican)

#Create a smoothed loess line with shaded error region 
lgbtq_loess_2016 <- ggplot(data = group_party_2016_binary_lgbtq, aes(y = lgbtqindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "LGBTQ+", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#MEDICAL INSURANCE
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_medinsur <- select(ALL_MERGED_ANES_2016, republican, whiteindex, medinsurindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), medinsurindex_mean = mean(medinsurindex), medinsurindex_sd = sd(medinsurindex)) %>%
  mutate(medinsurindex_se = medinsurindex_sd/sqrt(n)) %>%
  mutate(medinsurindex_confhi = medinsurindex_mean + (medinsurindex_se*1.96), 
         medinsurindex_conflow = medinsurindex_mean - (medinsurindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_medinsur$republican <- factor(group_party_2016_binary_medinsur$republican)

#Create a smoothed loess line with shaded error region
medinsur_loess_2016 <- ggplot(data = group_party_2016_binary_medinsur, aes(y = medinsurindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "Medical Insurance", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#ABORTION
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2016_binary_abortion <- select(ALL_MERGED_ANES_2016, republican, whiteindex, abortionindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), abortionindex_mean = mean(abortionindex), abortionindex_sd = sd(abortionindex)) %>%
  mutate(abortionindex_se = abortionindex_sd/sqrt(n)) %>%
  mutate(abortionindex_confhi = abortionindex_mean + (abortionindex_se*1.96), 
         abortionindex_conflow = abortionindex_mean - (abortionindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2016_binary_abortion$republican <- factor(group_party_2016_binary_abortion$republican)

#Create a smoothed loess line with shaded error region 
abortion_loess_2016 <- ggplot(data = group_party_2016_binary_abortion, aes(y = abortionindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth() +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "Abortion", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#Put the many loess lines into one graph
fig_d1 <- ggarrange(nationalism_loess_2016, racial_loess_2016, immigration_loess_2016, women_loess_2016, climate_loess_2016, lgbtq_loess_2016, medinsur_loess_2016, abortion_loess_2016,
                    labels = c("A", "B", "C", "D", "E", "F", "G", "H"),
                    ncol = 4, nrow = 2)
fig_d1

ggsave(fig_d1, filename = "Figures/FigureD1.tiff", height = 5, width = 9)

####################################################################
#Figure D2: Issue Attitudes by White Identity, 2020 Data
################################################################################

#NATIONALISM
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_nationalism <- select(ALL_MERGED_ANES_2020, republican, whiteindex, nationindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), nationindex_mean = mean(nationindex), nationindex_sd = sd(nationindex)) %>%
  mutate(nationindex_se = nationindex_sd/sqrt(n)) %>%
  mutate(nationindex_confhi = nationindex_mean + (nationindex_se*1.96), 
         nationindex_conflow = nationindex_mean - (nationindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_nationalism$republican <- factor(group_party_2020_binary_nationalism$republican)

#Create a smoothed loess line with shaded error region 
nationalism_loess_2020 <- ggplot(data = group_party_2020_binary_nationalism, aes(y = nationindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  #guides(color = FALSE, linetype = FALSE) +
  labs(title = "National Identity", x = element_blank(), y = "Mean Issue Position") + 
  scale_colour_manual(values = c("blue", "red"), labels = c("Democrat", "Republican"), guide = guide_legend(reverse = TRUE)) +
  scale_linetype_manual(values = c(1, 2), labels = c("Democrat", "Republican"), guide = guide_legend(reverse = TRUE)) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = c(.65, .2), legend.title = element_blank())

nationalism_loess_2020

#RACIAL RESENTMENT
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_racial <- select(ALL_MERGED_ANES_2020, republican, whiteindex, resentindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), resentindex_mean = mean(resentindex), resentindex_sd = sd(resentindex)) %>%
  mutate(resentindex_se = resentindex_sd/sqrt(n)) %>%
  mutate(resentindex_confhi = resentindex_mean + (resentindex_se*1.96), 
         resentindex_conflow = resentindex_mean - (resentindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_racial$republican <- factor(group_party_2020_binary_racial$republican)

#Create a smoothed loess line with shaded error region 
racial_loess_2020 <- ggplot(data = group_party_2020_binary_racial, aes(y = resentindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Racial Resentment", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red"))+
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")
racial_loess_2020

#IMMIGRATION
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_immigration <- select(ALL_MERGED_ANES_2020, republican, whiteindex, immigrationindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), immigrationindex_mean = mean(immigrationindex), immigrationindex_sd = sd(immigrationindex)) %>%
  mutate(immigrationindex_se = immigrationindex_sd/sqrt(n)) %>%
  mutate(immigrationindex_confhi = immigrationindex_mean + (immigrationindex_se*1.96), 
         immigrationindex_conflow = immigrationindex_mean - (immigrationindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_immigration$republican <- factor(group_party_2020_binary_immigration$republican)

#Create a smoothed loess line with shaded error region 
immigration_loess_2020 <- ggplot(data = group_party_2020_binary_immigration, aes(y = immigrationindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Immigration Policy", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#WOMEN
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_women <- select(ALL_MERGED_ANES_2020, republican, whiteindex, womenindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), womenindex_mean = mean(womenindex), womenindex_sd = sd(womenindex)) %>%
  mutate(womenindex_se = womenindex_sd/sqrt(n)) %>%
  mutate(womenindex_confhi = womenindex_mean + (womenindex_se*1.96), 
         womenindex_conflow = womenindex_mean - (womenindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_women$republican <- factor(group_party_2020_binary_women$republican)


#Create a smoothed loess line with shaded error region 
women_loess_2020 <- ggplot(data = group_party_2020_binary_women, aes(y = womenindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  theme (axis.text.y = element_blank(), axis.text.x = element_blank()) +
  labs(title = "Women's Equality", x = element_blank(), y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#CLIMATE
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_climate <- select(ALL_MERGED_ANES_2020, republican, whiteindex, climateindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), climateindex_mean = mean(climateindex), climateindex_sd = sd(climateindex)) %>%
  mutate(climateindex_se = climateindex_sd/sqrt(n)) %>%
  mutate(climateindex_confhi = climateindex_mean + (climateindex_se*1.96), 
         climateindex_conflow = climateindex_mean - (climateindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_climate$republican <- factor(group_party_2020_binary_climate$republican)

group_party_2020_binary_climate <- mutate(group_party_2020_binary_climate, partyname = if_else(republican == 0, "Democrat", "Republican"))

#Create a smoothed loess line with shaded error region 
climate_loess_2020 <- ggplot(data = group_party_2020_binary_climate, aes(y = climateindex_mean, x = whiteindex, color = partyname, linetype = partyname)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  #guides(color = FALSE) +
  scale_colour_manual(values = c("blue", "red")) +
  scale_linetype_manual(values = c("solid", "dashed")) +
  labs(title = "Climate Change", x = "White Identity Index", y = "Mean Issue Position", colour = element_blank(), linetype = element_blank()) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#LGBTQ+ 
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_lgbtq <- select(ALL_MERGED_ANES_2020, republican, whiteindex, lgbtqindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), lgbtqindex_mean = mean(lgbtqindex), lgbtqindex_sd = sd(lgbtqindex)) %>%
  mutate(lgbtqindex_se = lgbtqindex_sd/sqrt(n)) %>%
  mutate(lgbtqindex_confhi = lgbtqindex_mean + (lgbtqindex_se*1.96), 
         lgbtqindex_conflow = lgbtqindex_mean - (lgbtqindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_lgbtq$republican <- factor(group_party_2020_binary_lgbtq$republican)

#Create a smoothed loess line with shaded error region 
lgbtq_loess_2020 <- ggplot(data = group_party_2020_binary_lgbtq, aes(y = lgbtqindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  # geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "LGBTQ+", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#MEDICAL INSURANCE
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_medinsur <- select(ALL_MERGED_ANES_2020, republican, whiteindex, medinsurindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), medinsurindex_mean = mean(medinsurindex), medinsurindex_sd = sd(medinsurindex)) %>%
  mutate(medinsurindex_se = medinsurindex_sd/sqrt(n)) %>%
  mutate(medinsurindex_confhi = medinsurindex_mean + (medinsurindex_se*1.96), 
         medinsurindex_conflow = medinsurindex_mean - (medinsurindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_medinsur$republican <- factor(group_party_2020_binary_medinsur$republican)

#Create a smoothed loess line with shaded error region
medinsur_loess_2020 <- ggplot(data = group_party_2020_binary_medinsur, aes(y = medinsurindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth()  +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "Medical Insurance", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#ABORTION
#Create a new dataframe that has the conditional mean and standard deviation for each subgroup
group_party_2020_binary_abortion <- select(ALL_MERGED_ANES_2020, republican, whiteindex, abortionindex) %>%
  na.omit() %>%
  mutate(whiteindex = round(whiteindex, 2)) %>%
  group_by(republican, whiteindex) %>%
  summarise(n=n(), abortionindex_mean = mean(abortionindex), abortionindex_sd = sd(abortionindex)) %>%
  mutate(abortionindex_se = abortionindex_sd/sqrt(n)) %>%
  mutate(abortionindex_confhi = abortionindex_mean + (abortionindex_se*1.96), 
         abortionindex_conflow = abortionindex_mean - (abortionindex_se*1.96), 
         partylabel = if_else(republican == 1 & whiteindex == 1, "Republican", if_else(republican == 0 & whiteindex == 1, "Democrat", NA_character_)))

group_party_2020_binary_abortion$republican <- factor(group_party_2020_binary_abortion$republican)

#Create a smoothed loess line with shaded error region 
abortion_loess_2020 <- ggplot(data = group_party_2020_binary_abortion, aes(y = abortionindex_mean, x = whiteindex, color = republican, linetype = republican)) +
  geom_smooth() +
  ylim(-1, 1) +
  #geom_label(aes(label = partylabel), nudge_x = .05, na.rm = TRUE, label.size = NA, segment.color = NA, size = 3)+
  guides(color = FALSE, linetype = FALSE) +
  labs(title = "Abortion", x = "White Identity Index", y = element_blank()) + 
  scale_colour_manual(values = c("blue", "red")) +
  theme_bw() + theme(text = element_text(family = "serif"), panel.grid.minor = element_blank(), legend.position = "none")

#Put the many loess lines into one graph
fig_d2 <- ggarrange(nationalism_loess_2020, racial_loess_2020, immigration_loess_2020, women_loess_2020, climate_loess_2020, lgbtq_loess_2020, medinsur_loess_2020, abortion_loess_2020,
                    labels = c("A", "B", "C", "D", "E", "F", "G", "H"),
                    ncol = 4, nrow = 2)
fig_d2

ggsave(fig_d2, filename = "Figures/FigureD2.tiff", height = 5, width = 9)





